#include "recbox.h"
#include<QDebug>
#include "ui_recbox.h"
#include"recboxitem.h"
RecBox::RecBox(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::RecBox),
    row(1),
    col(4)
{
    ui->setupUi(this);
}

RecBox::~RecBox()
{
    delete ui;
}

void RecBox::initRecBoxItem(QJsonArray arr, int row)
{
   if(2==row)
   {
       this->row=row;
       col=8;
   }
   else
   {
       ui->recDownPage->hide();
   }

   jsonarr=arr;
   //在recbox控件添加recboxitem
   currentIndex=0;
   count=arr.size()/col;
   creatRecBoxItem();
}

void RecBox::creatRecBoxItem()//为recnoxitem添加图片和文本
{
    //添加新元素之前先清空旧元素
    QList<RecBoxItem*> downlist=ui->recDownPage->findChildren<RecBoxItem*>();
    for(auto e:downlist)
    {
        ui->recDownH->removeWidget(e);
        delete e;
    }
    QList<RecBoxItem*> uplist=ui->recUpPage->findChildren<RecBoxItem*>();
    for(auto e:uplist)
    {
        ui->recUpH->removeWidget(e);
        delete e;
    }
    int index=0;
   for(int i=currentIndex*col;i<col+currentIndex*col;i++)
   {
     RecBoxItem*item=new RecBoxItem();
     QJsonObject obj=jsonarr[i].toObject();
     item->setBoxItemText(obj.value("text").toString());
     item->setrecMusicImage(obj.value("path").toString());

     if(index>=col/2&&2==row)
     {
         ui->recDownH->addWidget(item);
     }
     else
     {
         ui->recUpH->addWidget(item);
     }
     index++;
   }
}

void RecBox::on_btUp_clicked()
{
   currentIndex--;
   if(currentIndex<0)
   {
       currentIndex=count-1;
   }

   creatRecBoxItem();
}

void RecBox::on_btDown_clicked()
{
    currentIndex++;
    if(currentIndex>=count)
    {
        currentIndex=0;
    }
    creatRecBoxItem();
}
